#include "src\precompiled.h"
#include "src\dialog.h"
#include "src\resource.h"

extern HINSTANCE g_hinstance;

template <> dialog *singleton<dialog>::m_singleton = 0;

dialog::dialog()
{ }

dialog::~dialog() { }

bool dialog::execute()
{
	HWND hwnd = GetCOREInterface()->GetMAXHWnd();
	INT res = DialogBoxParam(g_hinstance, MAKEINTRESOURCE(IDD_STARTDIALOG),
		hwnd, &dialog::DialogProc, ((LPARAM)this));
	return res != -1;
}

INT_PTR dialog::proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_INITDIALOG:
		m_hwnd = hwnd;
		return init();
	case WM_COMMAND:
		switch(LOWORD(wparam))
		{
		case IDC_RADIO1:
		case IDC_RADIO2:
		case IDC_RADIO3:
		case IDC_RADIO4:
		case IDC_RADIO5:
		case IDC_COMBO1:
		case IDC_CHECK1:
		case IDC_CHECK2:
		case IDC_CHECK3:
			return run(LOWORD(wparam));

		case IDOK:
			return done();

		case IDCANCEL:
			EndDialog(hwnd, -1);
			return TRUE;
		}
	}
	return FALSE;
}

INT_PTR CALLBACK dialog::DialogProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	dialog *dlg = 0;
	if(msg == WM_INITDIALOG && lparam != 0) {
		SetWindowLongPtr(hwnd, GWLP_USERDATA, lparam);
		dlg = (dialog*) lparam;
	}
	else
		dlg = (dialog*) GetWindowLongPtr(hwnd, GWLP_USERDATA);
	if(dlg != 0)
		return dlg->proc(hwnd, msg, wparam, lparam);
	return FALSE;
}

BOOL dialog::init()
{
	CheckRadioButton(m_hwnd, IDC_RADIO1, IDC_RADIO3, IDC_RADIO1);
	CheckRadioButton(m_hwnd, IDC_RADIO4, IDC_RADIO5, IDC_RADIO4);
	CheckRadioButton(m_hwnd, IDC_CHECK1, IDC_CHECK1, IDC_CHECK1);

	HWND hcb = GetDlgItem(m_hwnd, IDC_COMBO1);
	SendMessage(hcb, CB_ADDSTRING, 0, (LPARAM) "DXT1");
	SendMessage(hcb, CB_ADDSTRING, 0, (LPARAM) "DXT1 + DXT3");
	SendMessage(hcb, CB_ADDSTRING, 0, (LPARAM) "DXT1 + DXT5");
	SendMessage(hcb, CB_ADDSTRING, 0, (LPARAM) "None");
	SendMessage(hcb, CB_SETCURSEL, 0, 0);

	return TRUE;
}

BOOL dialog::run(int arg)
{
	HWND hcb = GetDlgItem(m_hwnd, IDC_COMBO1);
	HWND hc1 = GetDlgItem(m_hwnd, IDC_CHECK1);
	HWND hst = GetDlgItem(m_hwnd, IDC_STATIC1);
	BOOL params_enabled;
	switch(arg)
	{
	case IDC_RADIO1:
	case IDC_RADIO2:
	case IDC_RADIO3:
		CheckRadioButton(m_hwnd, IDC_RADIO1, IDC_RADIO3, arg);
		return TRUE;

	case IDC_RADIO4:
	case IDC_RADIO5:
		CheckRadioButton(m_hwnd, IDC_RADIO4, IDC_RADIO5, arg);
		params_enabled = (arg == IDC_RADIO4);
		EnableWindow(hcb, params_enabled);
		EnableWindow(hc1, params_enabled);
		EnableWindow(hst, params_enabled);
		return TRUE;

	case IDC_COMBO1:
		break;

	case IDC_CHECK1:
	case IDC_CHECK2:
	case IDC_CHECK3:
		break;
	}
	return FALSE;
}

BOOL dialog::done()
{
	if(IsDlgButtonChecked(m_hwnd, IDC_RADIO1) == BST_CHECKED)
		m_scene_option = SO_EXPORT_SCENE;
	else if(IsDlgButtonChecked(m_hwnd, IDC_RADIO2) == BST_CHECKED)
		m_scene_option = SO_EXPORT_WORLD_TM;
	else if(IsDlgButtonChecked(m_hwnd, IDC_RADIO3) == BST_CHECKED)
		m_scene_option = SO_EXPORT_LOCAL_TM;

	if(IsDlgButtonChecked(m_hwnd, IDC_RADIO4) == BST_CHECKED)
		m_convert_textures = true;
	else if(IsDlgButtonChecked(m_hwnd, IDC_RADIO5) == BST_CHECKED)
		m_convert_textures = false;

	if(IsDlgButtonChecked(m_hwnd, IDC_CHECK1) == BST_CHECKED)
		m_generate_mipmaps = true;
	else
		m_generate_mipmaps = false;

	m_compression_method = SendMessage(GetDlgItem(m_hwnd, IDC_COMBO1), CB_GETCURSEL, 0, 0);

	if(IsDlgButtonChecked(m_hwnd, IDC_CHECK2) == BST_CHECKED)
		m_node_anims = true;
	else
		m_node_anims = false;

	if(IsDlgButtonChecked(m_hwnd, IDC_CHECK3) == BST_CHECKED)
		m_skeletal_anims = true;
	else
		m_skeletal_anims = false;

	EndDialog(m_hwnd, 0);
	return TRUE;
}

int dialog::scene_export_option() const { return m_scene_option; }

int dialog::compression_method_option() const { return m_compression_method; }

bool dialog::convert_textures() const { return m_convert_textures; }

bool dialog::generate_mipmaps() const { return m_generate_mipmaps; }

bool dialog::export_animations() const { return m_node_anims; }

bool dialog::export_skeleton() const { return m_skeletal_anims; }